package com.right.health.app.ui.household.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.right.health.app.R;
import com.right.health.app.bean.BloodSugarBean;

import java.util.List;

public class BloodSugarLatestAdapter extends RecyclerView.Adapter<BloodSugarLatestAdapter.VH> {

    private Context mContext;
    private List<BloodSugarBean> mDatas;

    public BloodSugarLatestAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_blood_sugar_late, viewGroup, false);
        return new VH(v);
    }

    public void setData(List<BloodSugarBean> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull VH viewHolder, int i) {
        String key = getKey(i);
        String value = "--";
        if (!TextUtils.isEmpty(mDatas.get(i).getSugarvalue())) {
            value = mDatas.get(i).getSugarvalue();
        }
        if (TextUtils.equals(mDatas.get(i).getSugarresult(), "0") || (TextUtils.equals(mDatas.get(i).getSugarresult(), "2"))) {
            viewHolder.valueTv.setTextColor(mContext.getResources().getColor(R.color.color_E60012));
        } else {
            viewHolder.valueTv.setTextColor(mContext.getResources().getColor(R.color.color_111111));
        }
        viewHolder.keyTv.setText(key);
        viewHolder.valueTv.setText(value);
    }

    private String getKey(int i) {
        String key = "";
        switch (i) {
            case 0:
                key = "凌晨";
                break;
            case 1:
                key = "早餐前";
                break;
            case 2:
                key = "早餐后";
                break;
            case 3:
                key = "午餐前";
                break;
            case 4:
                key = "午餐后";
                break;
            case 5:
                key = "晚餐前";
                break;
            case 6:
                key = "晚餐后";
                break;
            case 7:
                key = "睡前";
                break;
        }
        return key;
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public static class VH extends RecyclerView.ViewHolder {
        TextView keyTv;
        TextView valueTv;

        public VH(View v) {
            super(v);
            keyTv = v.findViewById(R.id.tv_sugar_key);
            valueTv = v.findViewById(R.id.tv_sugar_value);
        }
    }
}
